<?php

/**
 * Product Class
 * @package mobi.application.modules.admin
 * @author xoannt
 */
class Product extends MY_Controller {
    protected $table = 'tbl_product';
    protected $col_id = 'pro_id';
    public function __construct() {
        parent::__construct();
        $this->load->model('category_model');
        $this->load->model('brand_model');
        $this->load->model('product_model');
        //Lấy đường dẫn url của thư mục chứa hình ảnh được upload
        $this->_img_path = base_url() . "publics/images/";
        //echo $this->_img_path;
        //Lấy đường dẫn vật lý của thư mục chứa hình ảnh đươc upload
        $this->_img_url = realpath(APPPATH . "../publics/images/");
    }
    public function index()
    {
        $data['list_product'] = $this->product_model->list_tab($this->table);
        $this->layout('product/index', $data);
    }
     public function set_validate()
    {
        $this->form_validation->set_rules('name', 'Product name', 'required');
        $this->form_validation->set_rules('info', 'Info', 'required');
        $this->form_validation->set_rules('price', 'Price', 'required|numeric');
        $this->form_validation->set_rules('country', 'Country', 'required');
        $this->form_validation->set_rules('category[]', 'Category', 'required');
        $this->form_validation->set_message('required', '%s  is not empty');
    }
     public function get_pro()
    {
        $name = $this->input->post('name');
        $info = $this->input->post('info');
        $price = $this->input->post('price');
        $sale = $this->input->post('sale');
        $country = $this->input->post('country');
        $brand = $this->input->post('brand');
        $date = getdate();
        $datenow = $date['year'] . "-" . $date['mon'] . "-" . $date['mday'];
        $data = array(
            'pro_name' => $name,
            'pro_info' => $info,
            'pro_price' => $price,
            'pro_sale' => $sale,
            'pro_date' => $datenow,
            'pro_country' => $country,
            'bra_id' => $brand
        );
         $date_check = $this->check_form->check_sql($data);
        return $date_check;
    }
    public function insert()
    {
        $this->set_validate('');
        $data['list_cate'] = $this->category_model->show_cate(0);
        $data['list_brand'] = $this->brand_model->list_tab('tbl_brand');
        if ($this->form_validation->run() === FALSE)
        {
            $this->layout('product/insert', $data);
        }
        else
        {
            $data_add = $this->get_pro();
            $this->product_model->action_tab($this->table, $data_add, '');
            $data_cate = $this->input->post('category');
            $pro_id = $this->product_model->get_id_max();
            $i = 1;
            foreach ($data_cate as $cate)
            {
                $data[$i] = array(
                    'cate_id' => $cate,
                    'pro_id' => $pro_id
                );
                $this->product_model->action_tab('tbl_cate_pro', $data[$i], '');
                $i++;
            }
            redirect('index.php/admin/product/index');
        }
    }
    public function edit($id = '')
    {
        $this->set_validate('');
        $data['list_cate'] = $this->category_model->show_cate(0);
        $data['list_brand'] = $this->brand_model->list_tab('tbl_brand');
        if(isset($id) && $id != '')
        {
            $data['list_product'] = $this->category_model->get_one($this->table, $this->col_id, $id);
            $data['cate_pro'] = $this->product_model->get_one('tbl_cate_pro', $this->col_id, $id);
            if ($this->form_validation->run() === FALSE)
            {
                $this->layout('product/edit', $data);
            }
            else
            {
                $data_edit = $this->get_pro();
                $this->product_model->action_tab($this->table, $data_edit, $this->col_id, $id);
                $data_cate = $this->input->post('category');
                $i = 1;
                $this->product_model->detele_one('tbl_cate_pro', $this->col_id, $id);
                foreach ($data_cate as $cate)
                {
                    $data[$i] = array(
                        'cate_id' => $cate,
                        'pro_id' =>$id
                    );
                    $this->product_model->action_tab('tbl_cate_pro', $data[$i], '');
                    $i++;
                }
                redirect('admin/product/index');
            }
        }
    }
    public function delete($id = '')
    {
        if(isset($id) && $id != '')
        {
           $this->product_model->detele_one($this->table, $this->col_id, $id);
           $this->product_model->detele_one('tbl_cate_pro', $this->col_id, $id);
          // $this->product_model->detele_one('tbl_slider', $this->_col_id, $id);
           $this->product_model->detele_one('tbl_pro_img', $this->col_id, $id);
           redirect('admin/product/index'); 
        }
    }
    public function manage_img($pro_id = '', $action = '', $id_img = '')
    {
        $url = "admin/product/manage_img/{$pro_id}";
        if(isset($pro_id) && $pro_id != '')
        {
            $data['pro_id'] = $pro_id;
            $data['pro_name'] = $this->product_model->get_info('pro_id', $pro_id, 'pro_name');
            $data['list_img'] = $this->product_model->list_img($pro_id);
            $data['img_main'] = $this->product_model->list_img($pro_id,1);
            $this->layout('product/img_index', $data);
            
            if($this->input->post('sub_main'))
            {
                if($this->product_model->check_img($pro_id) == TRUE)
                {
                   $this->product_model->detele_img($pro_id);
                }
                $info_img = $_FILES['img_main'];
                
                $path_img = $this->_img_url."/".$info_img['name'];
                move_uploaded_file($info_img['tmp_name'], $path_img);
                if($info_img['name'] != '')
                {
                    $data_main_img = array(
                        'pro_img_src' => $this->_img_path.$info_img['name'],
                        'pro_img_main' => 1,
                        'pro_id' => $pro_id
                    );
                    $this->product_model->action_tab('tbl_pro_img', $data_main_img, '');
                }
                redirect($url);
            }
            
            if($this->input->post('sub_imgs'))
            {
                $j = 1;
                foreach($_FILES['imgs']['tmp_name'] as $key => $tmp_name )
                {
                    $file_name = $_FILES['imgs']['name'][$key];
                    $file_tmp_name =$_FILES['imgs']['tmp_name'][$key];
                    $path_r = $this->_img_url."/".$file_name;
                    $path = $this->_img_path.$file_name;
                    move_uploaded_file($file_tmp_name,"$path_r");
                    if($file_name != '')
                    {
                        $data_img[$j] = array(

                                'pro_img_src' => $path,
                                'pro_img_main' => 0,
                                'pro_id'    => $pro_id,

                        );
                        $this->product_model->action_tab('tbl_pro_img',$data_img[$j],'');
                    }
                    $j++;  
                }
                redirect($url);
                
            }
            
            if($action == 'delete' && isset($id_img) && $id_img != '')
            {
//                $path_img = $this->product_model->get_info_img('id', $id_img, 'pro_img_src');
//                echo $path_img;
                // unlink();
                //$path_img =  $_SERVER['DOCUMENT_ROOT']."/publics/images";
                $this->product_model->detele_one('tbl_pro_img', 'id', $id_img);               
                redirect($url);
            }
        }
    }
    public function slider()
    {
        $data['list_product'] = $this->product_model->list_tab($this->table);
        $data['list_slider'] = $this->product_model->list_tab('tbl_slider');
        
        $this->layout('product/slider', $data);
        if($this->input->post())
        {
            $this->product_model->del_all();
            $product = $this->input->post('product');
            $order  = 1;
            foreach ($product as $pro)
            {
                $data_add = array(
                        'pro_id' => $pro,
                        'order' =>$order
                );
                $order++;
                
             $this->product_model->action_tab('tbl_slider', $data_add);
                        
            }
            redirect('admin/product');
        }
    }
    
    
}
 
